<template lang="html">
  <div class="rating" ref="rating">
    <div class="rating-content">
      <div class="overview">
        <div class="overview-left">
          <h1 class="score">{{seller.score}}</h1>
          <div class="title">综合评分</div>
          <div class="rank">高于周边商家{{seller.rankRate}}%</div>
        </div>
        <div class="overview-right">
          <div class="score-wrapper">
            <span class="title">服务态度</span>
            <star :size="36" :score="seller.serviceScore"></star>
            <span class="score">{{seller.serviceScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">商品评分</span>
            <star :size="36" :score="seller.foodScore"></star>
            <span class="score">{{seller.foodScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">送达时间</span>
            <span class="delivery">{{seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <split></split>
      <ratingselect :ratings="ratings" :selectType="selectType" :onlyContent="onlyContent" @select-type="_sellectType" @only-content="_onlyContent"></ratingselect>
      <div class="rating-wrapper">
        <ul>
          <li v-show="needShow(rating.rateType, rating.text)" v-for="rating in ratings" :key="rating.index" class="rating-item border-1px">
            <div class="avatar">
              <img :src="rating.avatar" width="28" height="28" alt="">
            </div>
            <div class="content">
              <h1 class="name">{{rating.username}}</h1>
              <div class="star-wrapper">
                <star :size="24" :score="rating.score"></star>
                <span class="delivery" v-show="rating.deliveryTime">{{rating.deliveryTime}}分钟送达</span>
              </div>
              <p class="text">{{rating.text}}</p>
              <div class="recommend" v-show="rating.recommend.length">
                <span :class="{'icon-thumb_up': rating.rateType === 0,'icon-thumb_down': rating.rateType === 1}"></span>
                <span class="item" v-for="item in rating.recommend" :key="item.index">{{item}}</span>
              </div>
              <div class="time">
                {{rating.rateTime | formatDate}}
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript0-6">
import BScorll from 'better-scroll';
import star from '@/components/star/star.vue';
import split from '@/components/split/split.vue';
import ratingselect from '@/components/ratingselect/ratingselect.vue';
import {formatDate} from '@/common/js/data.js';

const ERR_OK = 0;
const ALL = 2;

export default {
  name: 'rating',
  props: {
    seller: {
      type: Object
    }
  },
  components: {
    star,
    split,
    ratingselect
  },
  data() {
    return {
      ratings: [],
      selectType: ALL,
      onlyContent: true
    };
  },
  created() {
    this.$http.get('/api/ratings').then((response) => {
      response = response.body;
      if (response.errno === ERR_OK) {
        this.ratings = response.data;
        this.$nextTick(() => {
          this.scroll = new BScorll(this.$refs.rating, {
            click: true
          });
        });
      }
    });
  },
  methods: {
    _sellectType(type) {
      this.selectType = type;
      this.scroll.refresh();
    },
    _onlyContent(type) {
      this.onlyContent = type;
      this.scroll.refresh();
    },
    needShow(type, text) {
      if (this.onlyContent && !text) {
        return false;
      }

      if (this.selectType === ALL) {
        return true;
      } else {
        return this.selectType === type;
      }
    }
  },
  filters: {
    formatDate(time) {
      let date = new Date();
      return formatDate(date, 'yyyy-MM-dd hh:mm');
    }
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus">

  @import '../../common/stylus/mixin.styl';

  .rating
    position: fixed
    top: 174px
    left: 0
    bottom: 0
    width: 100%
    overflow: hidden
    .rating-content
      .overview
        display: flex
        padding: 18px 0
        .overview-left
          flex: 0 0 137 px
          width: 137px
          @media only screen and (max-width: 320px)
            flex: 0 0 120px
            width: 120px
          .score
            line-height: 28px
            font-size: 24px
            text-align: center
            color: rgb(255, 153, 0)
          .title
            margin-top: 6px
            line-height: 12px
            font-size: 12px
            text-align: center
            color: rgb(7, 17, 27)
          .rank
            margin: 8px 0 6px 0
            line-height: 10px
            font-size: 10px
            text-align: center
            color: rgb(147, 153, 159)
        .overview-right
          flex: 1
          padding: 6px 0 6px 24px
          @media only screen and (max-width: 320px)
            padding-left: 6px
          .score-wrapper
            margin-bottom: 8px
            font-size: 0
            &:last-child
              margin-bottom: 0
            .title, .score
              display: inline-block
              vertical-align: top
              line-height: 18px
              font-size: 12px
            .title
              color: rgb(7, 17, 27)
            .score
              color: rgb(255, 153, 0)
            .star
              display: inline-block
              margin: 0 12px
              font-size: 12px
              vertical-align: top
            .delivery
              margin-left: 12px
              line-height: 18px
              font-size: 12px
              color: rgb(147, 153, 159)
      .rating-wrapper
        padding: 0 18px
        .rating-item
          display: flex
          padding: 18px 0
          border-1px(rgba(7, 17, 27, .1))
        .avatar
          flex: 0 0 28px
          width: 28px
          img
            border-radius: 50%
        .content
          flex: 1
          position: relative
          margin-left: 12px
          .name
            line-height: 12px
            font-size: 10px
            color: rgb(7, 17, 27)
          .star-wrapper
            margin-top: 4px
            font-size: 0
            .star
              display: inline-block
              margin-right: 6px
            .delivery
              line-height: 12px
              font-size: 10px
              color: rgb(147, 153, 159)
          .text
            margin: 6px 0 8px 0
            line-height: 18px
            font-size: 12px
            color: rgb(7, 17, 27)
          .recommend
            font-size: 0
            .icon-thumb_up, .icon-thumb_down
              display: inline-block
              margin: 0 8px 4px 0
              line-height: 16px
              font-size: 12px
            .icon-thumb_up
              color: rgb(0, 160, 220)
            .icon-thumb_down
              color: rgb(183, 187, 191)
            .item
              display: inline-block
              padding: 0 6px
              margin: 0 8px 4px 0
              line-height: 16px
              font-size: 9px
              color: rgb(147, 153, 159)
              border-top: 1px solid rgba(7, 17, 27, .1)
              border-bottom: 1px solid rgba(7, 17, 27, .1)
          .time
            position: absolute
            top: 0
            right: 0
            line-height: 12px
            font-size: 10px
            color: rgb(147, 153, 159)
</style>
